<template>
  <div class="contain">
    <el-form :inline="true" class="head_form">
        <!-- 分类查看商品 -->
        <el-form-item label="选择分类">
          <el-select v-model="value" placeholder="选择分类">
            <el-option
              v-for="item in choose_cate"
              :key="item.value"
              :label="item.food_name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 搜索按钮 -->
        <!-- <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item> -->
        <el-form-item>
          <el-button icon="el-icon-search"  @click="onSubmit" circle></el-button>
        </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      :cell-style="isCenter"
      :header-cell-style="headCenter"
      border
      class="body_table">
      <el-table-column
        type="selection"
        width="50">
      </el-table-column>
      <el-table-column
        prop="food_name"
        label="商品名"
        width="150">
      </el-table-column>
      <el-table-column
        prop="food_pic"
        label="图片路径"
        width="350"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="food_msg"
        label="商品介绍"
        width="240"
        show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        prop="food_price"
        label="价格 /元"
        width="120">
      </el-table-column>
      <el-table-column
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
          <!-- <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-dialog title="商品编辑" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { get, post } from '../../assets/http'
export default {
  name: 'EditGood',
  data () {
    return {
      value: '',
      choose_cate: [],
      // choose_cate: [{ label: '缤纷奶茶', value: 'naicha' }, {label: '五彩可乐', value: 'kele'}],
      tableData: [
        // {food_name: '卡不奇诺', food_pic: 'http://q6zagsjbu.bkt.clouddn.com/hamburger.jpg', food_msg: '高热量,高能量', food_price: '16'}
      ],
      dialogFormVisible: false,
      form: {

      }
    }
  },
  mounted () {
    // 查询到分类信息后赋值第一个分类名称给value属性
    this.getCate()
    // this.editGood()
  },
  methods: {
    handleEdit (row) {
      // 显示弹窗 编辑商品信息
    },
    handleDelete (row) {
      // console.log(row)
      // 根据商品id删除
      this.$confirm('是否要删除该商品', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 确定
        var res1 = await post('store/good/del', {food_id: row.food_id, store_id: this.$route.query.store_id})
        console.log(res1.flag)
        if (res1.flag) {
          this.onSubmit()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    async onSubmit () {
      // 分类搜索
      // 1. 根据value查询分类信息
      // 2.根据分类信息查询对应的商品的集合
      var res = await get('store/getGoodsByCateValueAndStoreId', {value: this.value, store_id: this.$route.query.store_id})
      if (res.flag) {
        this.tableData = res.data
        // console.log(res.data)
      } else {
        this.$message({
          message: '请稍后再试',
          type: 'warning'
        })
      }
    },
    async  editGood () {
      // console.log(this.value)
      // 获取该店铺商品分类的信息  已废弃
      var res1 = await post('store/edit', {value: this.value, store_id: this.$route.query.store_id})
      if (res1.flag) {
        this.tableData = res1.data
      } else {
        this.$message({
          message: '网络拥堵 请稍后再试',
          type: 'warning'
        })
      }
    },
    getCate () {
      // 1.根据店铺id获取分类信息  赋值分类名称给value
      var storeId = this.$route.query.store_id
      if (storeId != null && storeId !== undefined) {
        get('/store/getCategoryByStoreId', {store_id: storeId}).then(res => {
        // console.log(res)
          if (res.flag) {
            this.choose_cate = res.data
            this.value = res.data[0].value
            this.onSubmit()
          } else {
            this.$message.error('请稍后再试')
          }
        })
      }
    },
    isCenter (row, column, rowIndex, columnIndex) {
      return 'text-align:center'
    },
    headCenter (row, rowIndex) {
      return 'text-align:center'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.contain{
  width: 70%;
  margin: 0px auto;
  position: relative;
}
.head_form{
  margin-left: 3%;
  margin-top:  1%;
}
.body_table{
  margin: 0px auto;
  width: 1010px;
}
</style>
